package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.domain.Permission;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.service.PermissionService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    /**
     * 新增权限
     *
     * @param permission
     * @return
     */
    @RequestMapping("/add")
    public Result insertPermission(@RequestBody Permission permission) {
        try {
            permissionService.insertPermission(permission);
        } catch (Exception e) {
            e.printStackTrace();
            //提示错误信息
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
        //提示成功消息
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }


    /**
     * 查询单项权限
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {

        Permission permission = permissionService.findPermissionById(id);

        if (permission == null) {
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        } else {
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, permission);
        }
    }

    /**
     * 分页查询 条件查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByPage")
    public PageResult findPermissionByPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = permissionService.findPermissionByPage(queryPageBean);
        return pageResult;
    }

    /**
     * 删除权限
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result deletePermission(Integer id) {
        try {
            permissionService.deletePermission(id);
        } catch (Exception e) {
            e.printStackTrace();
            //删除失败
            return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);
        }
        //删除成功
        return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }

    /**
     * 修改权限
     *
     * @return
     */
    @RequestMapping("/update")
    public Result updatePermission(@RequestBody Permission permission) {
        try {
            permissionService.updatePermission(permission);
        } catch (Exception e) {
            e.printStackTrace();
            //编辑 修改失败
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
        }
        //编辑成功
        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
    }

    /**
     * 查询所有权限
     *
     * @return
     */
    @RequestMapping("/findAllPermissions")
    public Result findAllPermissions() {
        List<Permission> allPermissions = permissionService.findAllPermissions();
        if (allPermissions == null) {
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        } else {
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, allPermissions);
        }

    }
}
